<?php
/* SVN FILE: $Id: item_ribbons.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Profile Song item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_ribbons.php $
*/

class item_ribbons extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Add an Ribbon";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Add an effect to your name with an addition to others.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['ribbon'] = "";
			$this->itemSettings['multiple'] = 0;
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$ribbons = array();
		if(!$this->item['item_id'])
		{
			$ribbons[0] = array('','');
		}
		foreach(new DirectoryIterator($this->settings['market_images_path']."/ribbons") as $file)
		{
			if($file->isFile() && preg_match("/(.jpg|.gif|.png)/",$file->getFilename()))
			{
				$ribbons[] = array($file->getFilename(),$file->getFilename());
			}
		}
		if($this->item['item_id'])
		{
			if(count($ribbons) == 0)
			{
				$this->registry->output->showError( $this->lang->words['add_some_ribbons'] );
			}
			$image = $this->settings['market_images_url']."/ribbons/".$this->itemSettings['ribbon'];
		}
		else
		{
			if(count($ribbons) == 1)
			{
				$this->registry->output->showError( $this->lang->words['add_some_ribbons'] );
			}
			$image = $ribbons[mt_rand(1,count($ribbons)-1)][0];
			$ribbons[0] = array($image,$this->lang->words['select_a_ribbon']);
			$image = $this->settings['market_images_url']."/ribbons/".$image;
		}
		
		$form['ribbon'] = ipsRegistry::getClass('output')->formDropdown('item[ribbon]', $ribbons, $this->itemSettings['ribbon']);
		$form['multiple'] = ipsRegistry::getClass('output')->formSimpleInput('item[multiple]', $this->itemSettings['multiple']);
		return <<<EOF
				<li>
					<label>{$this->lang->words['which_ribbon']}</label>
					{$form['ribbon']}
					<img src='{$image}' alt='{$this->lang->words['icon_alt']}' id='ribbonImg' />
<script type='text/javascript'>
function changeRibbon()
{
	$('ribbonImg').src = ipb.vars['market_images_url']+"/icons/"+$('item[ribbon]').getValue();
}
$('item[ribbon]').observe('change',changeRibbon);
$('item[ribbon]').observe('keyup',changeRibbon);
changeRibbon();
</script>
				</li>
				<li>
					<label>
						{$this->lang->words['max_ribbons']}
						<span class="desctext">{$this->lang->words['max_ribbons_desc']}</span>
					</label>
					{$form['multiple']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @todo
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['ribbon'] = $input['ribbon'];
		$setting['multiple'] = max(intval($input['multiple']),0);
		
		$item_info['item_config'] = serialize($setting);
		
		if($this->DB->checkForField("topic_ribbons", "members"))
		{
			$this->DB->changeField("members","topic_ribbons","ptopic_ribbonsp_song","TEXT NOT NULL","''");
		}
		else
		{
			$this->DB->addField("members","topic_ribbons","TEXT NOT NULL","''");
		}
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		if(count(marketRegistry::getItemsByFiles($this->item['filename'])) == 1)
		{
			$this->DB->dropField("members","topic_ribbons");
		}
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		$adminDir = CP_DIRECTORY;
		$edits = array();
		$edits['topics.php']['line'] = sprintf($this->lang->words['about_the_line_x'],2189);
		$edits['threaded.php']['line'] = sprintf($this->lang->words['about_the_line_x'],260);
		
		return <<<EOF
<div id='fileEditsTopicsPHP'>
	<div class='acp-box'>
		<h3>
			{$adminDir}/applications/forums/modules_public/forums/topics.php ({$this->lang->words['optional']})
		</h3>
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
			<tr>
				<th colspan='2'>{$edits['topics.php']['line']}</th>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['find']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="2">		/* Add custom fields join? */
		if( \$this->settings['custom_profile_topic'] == 1 )</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['under_the_function']}
				</td>
				<td>
					public function _getTopicDataLinear()
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_before']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="2">		\$_post_joins[0]['select'] .= ', m.topic_ribbons';
</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['it_will_look_like']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="4">		\$_post_joins[0]['select'] .= ', m.topic_ribbons';
		/* Add custom fields join? */
		if( \$this->settings['custom_profile_topic'] == 1 )</textarea>
				</td>
			</tr>
		</table>
	</div>
</div>
<br />
<div id='fileEditsThreadedPHP'>
	<div class='acp-box'>
		<h3>
			{$adminDir}/applications/forums/sources/classes/threaded.php ({$this->lang->words['optional']})
		</h3>
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
			<tr>
				<th colspan='2'>{$edits['threaded.php']['line']}</th>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					Find
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="2">		/* Reputation system enabled? */
		if( \$this->settings['reputation_enabled'] )</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['under_the_function']}
				</td>
				<td>
					public function _getTopicDataThreaded()
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_before']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="2">		\$_post_joins[0]['select'] .= ', m.topic_ribbons';
</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['it_will_look_like']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="4">		\$_post_joins[0]['select'] .= ', m.topic_ribbons';
		/* Reputation system enabled? */
		if( \$this->settings['reputation_enabled'] )</textarea>
				</td>
			</tr>
		</table>
	</div>
</div>
<br />
<div id='skinEditsGlobal'>
	<div class='acp-box'>
		<h3>
			{$this->lang->words['skin_template']}: {$this->lang->words['bit_skin_global']} ({$this->lang->words['optional']})
		</h3>
		<table width='100%' cellpadding='0' cellspacing='0' border='0' class='alternate_rows'>
			<tr>
				<th colspan='2'>userInfoPane</th>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['find']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="1"><if test="authorwarn:|:\$author['warn_percent'] !== NULL"></textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['add_before']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10"><php>
\$author['topic_ribbons'] = \$author['topic_ribbons'] ? unserialize(\$author['topic_ribbons']) : array();
\$author['topic_ribbons'] = is_array(\$author['topic_ribbons']) ? \$author['topic_ribbons'] : array();
</php>
<if test="is_array(\$author['topic_ribbons']) && count(is_array(\$author['topic_ribbons']))">
	<div>
<foreach loop="\$author['topic_ribbons'] as \$ribbons">
<foreach loop="\$ribbons as \$ribbon">
		<img src='{\$this->settings['board_url']}/images/market/ribbons/{\$ribbon}' alt='' class='left' />
</foreach>
</foreach>
	</div>
</if>
</textarea>
				</td>
			</tr>
			<tr>
				<td style='white-space:nowrap;width:1px;min-width:1%'>
					{$this->lang->words['it_will_look_like']}
				</td>
				<td>
					<textarea style="width: 98%;" cols="80" rows="10"><if test="marketMoney:|:isset(\$author[\$this->settings['market_members_field']]) && (\$this->memberData['member_id'] || \$this->settings['market_guests']) && (\$this->memberData['member_group_id'] == \$this->settings['admin_group'] || \$this->settings['market_show_money'])">
<php>
if(!class_exists('marketRegistry'))
{
	require_once( IPSLib::getAppDir('ibmarket')."/sources/marketRegistry.php");
	ipsRegistry::member()->setProperty('market_deduct', 1);
	marketRegistry::__setup();
}
</php>
			<li>
				<span class='ft'>{\$this->settings['market_currency']}:</span>
				<span class='fc'>{parse market_currency="\$author[$this->settings['market_members_field']]"}</span>
			</li>

</if>
		<if test="authorcfields:|:\$author['custom_fields'] != """></textarea>
				</td>
			</tr>
		</table>
	</div>
</div>
EOF;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		$ribbons = $this->memberData['topic_ribbons'] ? unserialize($this->memberData['topic_ribbons']) : array();
		$ribbons = is_array($ribbons) ? $ribbons : array();
		
		if(!isset($ribbons[$this->item['item_id']]) || !is_array($ribbons[$this->item['item_id']]))
		{
			$ribbons[$this->item['item_id']] = array();
		}
		if($this->itemSettings['multiple'] && count($ribbons[$this->item['item_id']]) + 1 > $this->itemSettings['multiple'])
		{
			$this->registry->getClass('output')->showError('too_many_ribbons');
		}
		$ribbons[$this->item['item_id']][] = $this->itemSettings['ribbon'];
		
		IPSMember::save( $this->memberData['member_id'], array( 'core' => array( 'topic_ribbons' => serialize($ribbons) ) ) );
		
		$this->deleteItems($invItem);
		
		$this->writeLog($invItem, "Added ribbon <img src='".$this->settings['market_images_url']."/ribbons/".$this->itemSettings['ribbon']."' alt='' />");
		
		$this->registry->getClass('output')->redirectScreen( $this->lang->words['ribbon_added'], $this->settings['base_url_with_app'] . "module=inventory" );
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
